require 'rubygems'
require 'sinatra'
require 'bundler'
# Setup load paths
Bundler.require
$: << File.expand_path('../', __FILE__)
$: << File.expand_path('../lib', __FILE__)

require 'dotenv'
Dotenv.load

# Require base
require 'sinatra/base'
require 'active_support/core_ext/string'
require 'active_support/core_ext/array'
require 'active_support/core_ext/hash'
require 'active_support/json'

libraries = Dir[File.expand_path('../lib/**/*.rb', __FILE__)]
libraries.each do |path_name|
  require path_name
end
require 'app/extensions'
require 'app/models'
require 'app/helpers'
require 'app/routes'

module SinatraDemo
  class App < Sinatra::Application
    configure do
      set :database, lambda {
        db_name = ENV['MOPAAS_POSTGRESQL17678_NAME']||'ddthss5f2ke36t'
        host_name = ENV['MOPAAS_POSTGRESQL17678_HOST']||'ec2-54-225-136-187.compute-1.amazonaws.com'
        port = ENV['MOPAAS_POSTGRESQL17678_PORT']||'5432'
        user_name = ENV['MOPAAS_POSTGRESQL17678_USERNAME']||'etwyiclzyogecj'
        password = ENV['MOPAAS_POSTGRESQL17678_PASSWORD']||'RB6mGnFJ-V9Fr8pTmfYuLNpYM-'
        "postgres://#{user_name}:#{password}@#{host_name}:#{port}/#{db_name}"
        # ENV['DATABASE_URL'] || 'postgres://dbuser:z321318@localhost:5432/exampledb'
        # ENV['DATABASE_PQ_URL']||'postgres://uc687502e02374df994a1337c323db416:p45b0763e53304f0ba930534ee6478581@10.4.7.20:5433/d2b71bcf422234eb0a07d70684d06169c'
      }
    end

    configure :development, :staging do
      database.loggers << Logger.new(STDOUT)
    end

    configure do
      disable :method_override
      disable :static
      set :erb, escape_html: true
      set :sessions, httponly: true, secure: production?,
          secure: false, expire_after: 5.years, secret: ENV['SESSION_SECRET']
    end

    use Rack::Deflater
    use Rack::Standards
    use Routes::Static
    unless settings.production?
      use Routes::Assets
    end

    # Other routes:
    use Routes::Albums
    use Routes::Artists
  end
end
include SinatraDemo::Models
include SinatraDemo::Helpers